<template>
  <div>
    <cube-form 
     :model="model"
     :schema="schema"
      @submit.prevent="handleLogin"
      @validate="handleValidate"
      >
    </cube-form>
  </div>
</template>

<script>
export default {
   data(){

      return {
        schema:{
          //用数据来描述表单的形式
          fields:[
             {
               type:'input',
               modelKey:'username',
               label:"用户名",
               props:{placeholder:'请输入用户名'},
               rules:{
                 required:true
               },
               trigger:"focus"
             },
              {
                type:'input',
                modelKey:'password',
                label:"密码",
                props:{placeholder:'密码',type:"password",eye:{open:true}},
                rules:{
                  required:true
                }
             },
              {
               type:'submit',
               label:'登录'
              
             }
          ]
        },
        model:{
          username:"",
          password:""
        }
      }
   },
   methods:{
     handleLogin(){
        //登录请求
        this.$store.dispatch('login',this.model).then(res=>{
            //console.log(res)
            if(res){
               const path=this.$route.query.redirect||'/';
               this.$router.push(path)
            }
        }).catch(error=>{
             const toast=this.$createToast({
                time:2000,
                txt:'登录失败',
                type:'error'
             }).show();

        })
     },
     handleValidate(ret){
       //ret--校验结果
       //console.log(ret)
     }
   }
};
</script>

<style></style>
